package com.ryl.yomall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ryl.yomall.model.enums.PaymentStatus;
import com.ryl.yomall.model.order.OrderInfo;
import com.ryl.yomall.model.payment.PaymentInfo;
import com.ryl.yomall.payment.mapper.PaymentInfoMapper;
import com.ryl.yomall.payment.service.PaymentService;
import com.ryl.yomall.rabbitmq.constant.MQConstant;
import com.ryl.yomall.rabbitmq.service.RabbitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class PaymentServiceImpl implements PaymentService {


    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private RabbitService rabbitService;


    @Override
    public void savePaymentInfo(OrderInfo orderInfo,String paymentType) {
        //先查询是否有支付订单
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<> ();
        queryWrapper.eq("order_id", orderInfo.getId());
        queryWrapper.eq("payment_type", paymentType);
        Integer count = paymentInfoMapper.selectCount(queryWrapper);
        if(count > 0) return;

        // 保存交易记录
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date ());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentStatus( PaymentStatus.UNPAID.name());
        paymentInfo.setSubject(orderInfo.getTradeBody());
        paymentInfo.setTotalAmount(orderInfo.getTotalAmount());

        paymentInfoMapper.insert(paymentInfo);
    }

    //更新支付表的信息
    @Override
    public void updatePaymentInfo(Map<String, String> paramsMap) {
        //1:查询
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
                .eq("out_trade_no", paramsMap.get("out_trade_no")));
        if(null != paymentInfo && paymentInfo.getPaymentStatus().
                equals(PaymentStatus.UNPAID.name())){
            //更新支付状态
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            //流水号
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            //回调时间
            paymentInfo.setCallbackTime(new Date());//接收到回调
            //回调内容
            paymentInfo.setCallbackContent( JSONObject.toJSONString(paramsMap));
            paymentInfoMapper.updateById(paymentInfo);
            //MQ 通知订单微服务 更新订单状态 ,不在同一个库,使用mq
            //同时更新订单状态
            rabbitService.sendMessage( MQConstant.EXCHANGE_DIRECT_PAYMENT_PAY,
                    MQConstant.ROUTING_PAYMENT_PAY,paymentInfo.getOrderId());

           /* 在同一个库可以这样操作使用本地事务
            OrderInfo orderInfo = orderInfoMapper.selectById(paymentInfo.getOrderId());
            if(null != orderInfo && orderInfo.getOrderStatus().equals( OrderStatus.UNPAID.name())){
                orderInfo.setProcessStatus( ProcessStatus.PAID.name());
                orderInfo.setOrderStatus(OrderStatus.PAID.name());
                orderInfoMapper.updateById(orderInfo);
                //MQ 生产端
                rabbitService.sendMessage(RabbitConst.EXCHANGE_DIRECT_WARE_STOCK,
                        RabbitConst.ROUTING_WARE_STOCK,
                        JSONObject.toJSONString(initWareOrder(orderInfo.getId())));
            }

            */

        }

    }
}
